/**
 * 
 * @author Sunix
 * @date   2013-5-21
 * @Email  SunixSky@gmail.com
 * 
 */	
package view.components{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.utils.StringUtil;
	
	import manager.ConfigManager;
	import manager.LoggerManager;
	
	import scaleform.clik.controls.Button;
	import scaleform.clik.controls.Window;
	
	import utils.FileUtil;

	public class SDKOperator extends Window{
		
		public var sdkPath:FileBrowse;   //目录浏览
		public var sureBtn:Button;     //确定按钮
		public var path:String;      //SDK路径
		
		public function SDKOperator(){
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
		}
		
		protected function onAddedToStage(event:Event):void{
			veiwInit();
			eventInit();
		}
		
		private function eventInit():void{
			sureBtn.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		protected function onClick(event:MouseEvent):void{
			var bool:Boolean = validCheck();	
			if(!bool)
				return;
			writeConfigIni();
			writeXml();
		}
		
		protected function writeConfigIni():void{
			var string:String = "[config]\r\nsdk="+ sdkPath.filePath.text +"\\mxmlc.exe\r\nquality=80"
			var path:String = File.applicationDirectory.nativePath + "\\config.ini";
			FileUtil.write(string,path);
			ConfigManager.init = true;
			LoggerManager.log("保存SDK路径成功");
			super.onCloseButtonClick(null);
		}
		
		protected function writeXml():void{
			var xml:XML = <info init='1' ></info>;
			var path:String = File.applicationDirectory.nativePath + "\\assets/config/config.xml";
			FileUtil.write(xml.toXMLString(),path);
		}
		
		protected function validCheck():Boolean{
			if(!sdkPath.file){
				LoggerManager.error("目标路径不能为空");
				return false;
			}
			path = StringUtil.trim(sdkPath.file.nativePath);
			if(!path){ 
				LoggerManager.error("目标路径不能为空");
				return false;
			}
			return true;
		}
		
		private function veiwInit():void{
			if(!ConfigManager.init)
				title = "首次使用，请选择SDK路径";
			else
				title = "配置"
			sdkPath.browse.label = "浏览";
			
		}
	}
}